define([
        'app/ui/LecturePageVideoDialogUI',
        'underscore',
        'app/service/GeneralServices',
        'app/utils/WebUtils',
        'app/utils/CommonUtils'
        ], function (
        LecturePageVideoDialogUI,
		_,
		GeneralServices,
		WebUtils
    ) {
	
    var s = {};
	
    
    function fixImgHeight(ratio){
    	var w = $(".am-gallery-item-pic").width();
    	$(".am-gallery-item-pic").height(parseInt(w*ratio));
    	
    	var w2 = $(".am-modal-item-pic").width();
    	$(".am-modal-item-pic").height(parseInt(w2*ratio));
    }
    function updateLectureRatio(ratio, useruuid, roomuuid){
    	var p = {
    		useruuid:useruuid,
    		roomuuid:roomuuid,
    		ratio:ratio
    	}
    	GeneralServices.wjt_lecture_ratio_update(p, function(resp){
    		
    	});
    }
    
    function renderNumSelect(num ,id){
    	var el = '<select class="wjt-room-lecturepage-order" data-id="'+id+'">';
    	for(var i = 1; i<= 100; i++){
    		if(num == i){
    			el = el + '<option selected="selected" value="'+i+'">'+i+'</option>';
    		}else{
    			el = el + '<option value="'+i+'">'+i+'</option>';
    		}
    	}
    	el = el + '</select>';
    	return el;
    }
    function renderType(type){

		if(type=="img"){
			return "图片";
		}else if(type == "video"){
			return "视频";
		}else if(type == "live"){
			return "视频";
		}else if(type == "tube"){
			return "外站";
		}
    }
    function renderOperate(item){
    	
    	var el1 = 
    		'<li><a data-id="'+item.id+'"  class="wjt-room-lecturepage-del" href="javascript:void(0);">删除</a></li>';
    	
    	var el2 = 
        	'<li><a id="room_lecturepage_'+item.id+'" data-id="'+item.id+'" data-pageurl="'+item.pageurl+'" data-thumburl="'+item.thumburl+'"  class="wjt-room-lecturepage-edit" href="javascript:void(0);">编辑</a></li>'+
        	'<li><a data-id="'+item.id+'" class="wjt-room-lecturepage-del"  href="javascript:void(0);">删除</a></li>';
    	
    	if(item.type=="img"){
			return el1;
		}else if(item.type == "video" || item.type == "live"){
			return el2;
		}else if(item.type == "tube"){
			return el1;
		}
    }
    
    function del_lecturepage(){
    	var id = $(this).data("id");
    	
    	var p ={
    		id:id
    	};
    	
    	WebUtils.confirm("是否要删除此页面",function(){
    		//OK
        	GeneralServices.lecture_page_delete(p,function(resp){
        		$("#lecture_page_"+resp.result.id).remove();
        	});    		
    	},function(){
    		//cancel
    	});

    	
    }
    function order_lecturepage(){
    	var id = $(this).data("id");
    	var neworder = $(this).val();
    	var p={
    		id:id,
    		num:neworder
    	};
    	GeneralServices.lecture_page_order_update(p,function(resp){
    		if(resp.status == "200"){
    			$.setUrlParam("tab","3");
    		}
    	}); 
    }
    function edit_lecturepage(){
    	var id = $(this).data("id");
    	var thumburl = $(this).data("thumburl");
    	var pageurl = $(this).data("pageurl");
    	
    	LecturePageVideoDialogUI.open(id, pageurl, thumburl);
    	console.log(id);
    	console.log(thumburl);
    	console.log(pageurl);
    }
    
    function renderPic(lp){
    	if(lp.type == "img"){
    		return lp.pageurl;
    	}else if(lp.type == "video" || lp.type == "live"){
    		return lp.thumburl;
    	}else{
    		return "";
    	}
    }
    
    function renderLecturePages(lectures, roomuuid){
    	$(".wjt-lecture-gallery").html("");
    	for(var i = 0; i< lectures.length ; i++){
    		var item = lectures[i];

    		var el_pic = renderPic(item);
    		var el_num = renderNumSelect(item.num, item.id);
    		var el_type = renderType(item.type);
    		var el_operate = renderOperate(item);
    		
    		
    		var el = $(
    			'<li id="lecture_page_'+item.id+'">'+
			        '<div class="am-gallery-item">'+
			        	'<img class="am-gallery-item-pic" src="'+el_pic+'"  alt=""/>'+
		                '<h3 class="am-gallery-title">'+
		                	'<div class="am-g">'+
		                		'<div class="am-u-sm-6">排序：'+el_num+
								'<div class="am-u-sm-6" style="text-align:right;">'+
								'</div>'+
		                	'</div>'+
		                '</h3>'+
		                '<div class="am-gallery-desc">'+
							'<div class="am-g">'+
								'<div class="am-u-sm-6">类型：'+el_type+'</div>'+
								'<div class="am-u-sm-6" style="text-align: right;margin-top: -20px;">'+
									'<div class="am-dropdown  am-dropdown-up" data-am-dropdown="">'+
					                  '<button class="am-btn am-btn-default am-btn-xs am-dropdown-toggle" data-am-dropdown-toggle=""><span class="am-icon-cog"></span> <span class="am-icon-caret-up"></span></button>'+
					                  '<ul class="am-dropdown-content">'+
					                    el_operate+
					                  '</ul>'+
					                '</div>'+
			                	'</div>'+
		                	'</div>'+
		                '</div>'+
			        '</div>'+
			      '</li>');
    		$(".wjt-lecture-gallery").append(el);
    	}
    	
    	
    	$(".wjt-room-lecturepage-del").click(del_lecturepage);
    	$(".wjt-room-lecturepage-edit").click(edit_lecturepage);
    	$(".wjt-room-lecturepage-order").change(order_lecturepage);
    	
		//下拉栏
		$('.am-dropdown').dropdown();
    }
    
	s.initLecturePanel=function(ratio, lectures, useruuid, roomuuid){
		$("#wjt_add_video_btn").click(function(){
			LecturePageVideoDialogUI.open();
		});
		
		renderLecturePages(lectures, roomuuid);
		
		if(ratio == "0.75"){
			$(".room-lecture-ratio-group").find("label").removeClass("am-active");
			$(".room-lecture-ratio-group").find("label:last").addClass("am-active");
			$(".room-lecture-ratio-group").find("label").find("input").removeAttr("checked");
			$(".room-lecture-ratio-group").find("label:last").find("input").prop("checked","checked");
		}else{
			$(".room-lecture-ratio-group").find("label").removeClass("am-active");
			$(".room-lecture-ratio-group").find("label:first").addClass("am-active");
			$(".room-lecture-ratio-group").find("label").find("input").removeAttr("checked");
			$(".room-lecture-ratio-group").find("label:first").find("input").prop("checked","checked");
		}
		fixImgHeight(parseFloat(ratio));
		
		
		var $radios = $('[name="room-lecture-ratio-options"]');
	    $radios.on('change',function() {
	    	if($radios.filter(':checked').val() == "0.5625"){
	    		fixImgHeight(0.5625);
	    		updateLectureRatio(0.5625, useruuid, roomuuid);
	    	}else{
	    		fixImgHeight(0.75);
	    		updateLectureRatio(0.75, useruuid, roomuuid);
	    	}
	    });
	    
	    LecturePageVideoDialogUI.initDialog(roomuuid);
		
	};
	
	
	return s;

});
